package org.scboy.dbms.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.BaseAreaCodeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("base/areacode")
@Menu("微服务管理系统.全国区域编码")
public class BaseAreaCodeController {
	
	@Autowired
	BaseAreaCodeService service;

	@PutMapping("tree")
	@Permission("获取下级区域编码")
	public Result tree(@RequestBody Model params){
		return Result.Success(service.getTree(params));
	}
	
	@PostMapping("page")
	@Permission("分页获取")
	public Result page(@RequestBody Model params) {
		Page page=service.pageAreaCode(params);
		return Result.Success(page);
	}
	
	@GetMapping("get")
	@Permission("获取地区")
	public Result get(String id){
		return Result.Success(service.get(id));
	}

	@PostMapping("init")
	@Permission("初始化全国地址信息")
	public Result init(@RequestBody Model params) {
		service.init(params.getString("url"),params.getString("type"));
		return Result.Success("BASE_AREA_CODE_INIT_SUCCESS");
	}
	
	@PostMapping("addmapping")
	@Permission("新增地区映射")
	public Result addMapping(String code,String mappingCode,String mappingName,String areaId){
		if(service.addMapping(code,mappingCode,mappingName,areaId)) {
			return Result.Success("BASE_AREA_MAPPING_ADD_SUCCESS");
		}else {
			return Result.Success("BASE_AREA_MAPPING_ADD_ERROR");
		}
	}

	@PutMapping("updatemapping")
	@Permission("编辑地区映射")
	public Result updatemapping(String id,String areaId){
		if(service.updateMapping(id,areaId)) {
			return Result.Success("BASE_AREA_MAPPING_UPDATE_SUCCESS");
		}else {
			return Result.Success("BASE_AREA_MAPPING_UPDATE_ERROR");
		}
	}
	
	@DeleteMapping("deletemapping")
	@Permission("删除地区映射")
	public Result deleteMapping(String id){
		if(service.deleteMapping(id)) {
			return Result.Success("BASE_AREA_MAPPING_DELETE_SUCCESS");
		}else {
			return Result.Success("BASE_AREA_MAPPING_DELETE_ERROR");
		}
	}
	

	@PostMapping("mappingpage")
	@Permission("分页查询地区映射")
	public Result mappingPage(@RequestBody Model params){
		Page page=service.page(params);
		return Result.Success(page.getData());
	}
}
